package com.yklx.youke.admin.controller;

import com.yklx.youke.util.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;
import java.util.stream.Collectors;
import java.util.Objects;

import static java.util.Arrays.asList;

@RestController
@RequestMapping("/api/admin")
public class ApiEndpointController {

    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    /**
     * 获取所有API端点
     * 为了演示目的，返回一个预定义的API端点列表
     */
    @GetMapping("/api-endpoints")
    public ResponseEntity<ApiResponse<List<Map<String, Object>>>> getApiEndpoints() {
        try {
            List<Map<String, Object>> endpoints = new ArrayList<>();
            
            // 预定义的API端点列表
            addEndpoint(endpoints, "/api/admin/users", asList("GET", "POST", "PUT", "DELETE"), "UserController", "manageUsers", asList("user:read", "user:write"));
            addEndpoint(endpoints, "/api/admin/roles", asList("GET", "POST", "PUT", "DELETE"), "RoleController", "manageRoles", asList("role:read", "role:write"));
            addEndpoint(endpoints, "/api/admin/user-roles", asList("GET", "POST", "DELETE"), "UserRoleController", "manageUserRoles", asList("user-role:read", "user-role:write"));
            addEndpoint(endpoints, "/api/admin/courses", asList("GET", "POST", "PUT", "DELETE"), "CourseController", "manageCourses", asList("course:read", "course:write"));
            addEndpoint(endpoints, "/api/admin/schedules", asList("GET", "POST", "PUT", "DELETE"), "ScheduleController", "manageSchedules", asList("schedule:read", "schedule:write"));
            addEndpoint(endpoints, "/api/admin/bookings", asList("GET", "POST", "PUT", "DELETE"), "BookingController", "manageBookings", asList("booking:read", "booking:write"));
            addEndpoint(endpoints, "/api/admin/orders", asList("GET", "POST", "PUT", "DELETE"), "OrderController", "manageOrders", asList("order:read", "order:write"));
            addEndpoint(endpoints, "/api/admin/youbi", asList("GET", "POST", "PUT", "DELETE"), "YoubiController", "manageYoubi", asList("youbi:read", "youbi:write"));
            addEndpoint(endpoints, "/api/admin/menus", asList("GET", "POST", "PUT", "DELETE"), "MenuController", "manageMenus", asList("menu:read", "menu:write"));
            addEndpoint(endpoints, "/api/admin/permissions", asList("GET", "POST", "PUT", "DELETE"), "PermissionController", "managePermissions", asList("permission:read", "permission:write"));
            addEndpoint(endpoints, "/api/admin/messages", asList("GET", "POST", "PUT", "DELETE"), "MessageController", "manageMessages", asList("message:read", "message:write"));
            addEndpoint(endpoints, "/api/admin/settings", asList("GET", "POST", "PUT"), "AdminController", "manageSettings", asList("system:read", "system:write"));
            
            // 按URL排序
            endpoints.sort(Comparator.comparing(map -> (String) map.get("url")));

            return ResponseEntity.ok(ApiResponse.success(endpoints));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(ApiResponse.error("获取API端点失败: " + e.getMessage()));
        }
    }
    
    // 辅助方法添加端点信息
    private void addEndpoint(List<Map<String, Object>> endpoints, String url, List<String> methods, 
                           String controller, String method, List<String> permissions) {
        Map<String, Object> endpointInfo = new HashMap<>();
        endpointInfo.put("url", url);
        endpointInfo.put("methods", methods);
        endpointInfo.put("controller", controller);
        endpointInfo.put("method", method);
        endpointInfo.put("permissions", permissions);
        endpoints.add(endpointInfo);
    }

    /**
     * 根据URL和HTTP方法确定权限
     */
    private String determinePermissionForUrlAndMethod(String url, String method) {
        // 根据URL路径和HTTP方法推断权限
        if (url.contains("/api/admin/users")) {
            if (method.equals("GET")) return "user:read";
            else return "user:write";
        } else if (url.contains("/api/admin/roles")) {
            if (method.equals("GET")) return "role:read";
            else return "role:write";
        } else if (url.contains("/api/admin/user-roles")) {
            if (method.equals("GET")) return "user-role:read";
            else return "user-role:write";
        } else if (url.contains("/api/admin/courses")) {
            if (method.equals("GET")) return "course:read";
            else return "course:write";
        } else if (url.contains("/api/admin/schedules")) {
            if (method.equals("GET")) return "schedule:read";
            else return "schedule:write";
        } else if (url.contains("/api/admin/bookings")) {
            if (method.equals("GET")) return "booking:read";
            else return "booking:write";
        } else if (url.contains("/api/admin/orders")) {
            if (method.equals("GET")) return "order:read";
            else return "order:write";
        } else if (url.contains("/api/admin/youbi")) {
            if (method.equals("GET")) return "youbi:read";
            else return "youbi:write";
        } else if (url.contains("/api/admin/menus")) {
            if (method.equals("GET")) return "menu:read";
            else return "menu:write";
        } else if (url.contains("/api/admin/permissions")) {
            if (method.equals("GET")) return "permission:read";
            else return "permission:write";
        } else if (url.contains("/api/admin/messages")) {
            if (method.equals("GET")) return "message:read";
            else return "message:write";
        } else if (url.contains("/api/admin/settings")) {
            if (method.equals("GET")) return "system:read";
            else return "system:write";
        }

        // 对于无法识别的路径，返回null，不生成权限
        return null;
    }
}